<template>
  <ul class="todo-list">
    <!-- completed: 完成的类名 -->
    <li v-for="item in showList" :key="item.id" :class="{ completed: item.isDone }" >
      <div class="view">
        <input @input="done(item.id,$event)" :checked="item.isDone" class="toggle" type="checkbox" />
        <label>{{ item.name }}</label>
        <button @click="del(item.id)" class="destroy"></button>
      </div>
    </li>
  </ul>
</template>

<script>
import { mapState , mapGetters } from 'vuex' 

export default {
  methods: {
    del(id) {
      this.$store.commit('todoList/todoRemove',{id})
    },
    done(id,e) {
      this.$store.commit('todoList/todoDone',{id,check:e.target.checked})
    }
  },
  computed: {
    ...mapState('todoList',['list']),
    ...mapGetters('todoList',['showList']),
  },
  watch: {
    list: {
      deep: true,
      handler() {
        // 当数组的数据变化时存入本地
        localStorage.setItem('todolist', JSON.stringify(this.list))
      }
    }
  }
}
</script>